<?php namespace app\api\controller;

use app\common\controller\AuthAndBind;
use think\Controller;
use think\Db;
use alisms\SendSms;
use esign\EsignHandle;

/**
 * api
 */
class Identify extends AuthAndBind{
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 用户申请鉴定售后（退货/退款）
	 */
	public function userRequestRefund(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'identify_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['identify_id']];
				$where[] = ['create_user_id', 'eq', $in['user_id']];
				$oldData = Db::table("gd_identify") -> where($where) -> find();
				if( empty($oldData) ) returnResults2('-1', '操作失败,原数据有误');
				if( $oldData['refund_type'] != 0 && in_array($oldData['refund_applyfor_status'], [1, 2]) ) returnResults2('-1', '操作失败,不能重复申请鉴定售后');
				if( in_array($oldData['status'], [2, 3, 5]) ) returnResults2('-1', '操作失败,鉴定状态有误');
				
				$refund_applyfor_file = '';
				if( isset($in['refund_applyfor_file']) ) $refund_applyfor_file = is_array($in['refund_applyfor_file']) ? json_encode($in['refund_applyfor_file'], JSON_UNESCAPED_UNICODE) : '';
				
				$save['refund_type'] = 2;
				$save['refund_applyfor_price'] = $oldData['appralsal_fee'];
				$save['refund_applyfor_file'] = $refund_applyfor_file;
				$save['refund_applyfor_reason'] = isset($in['refund_applyfor_reason']) ? $in['refund_applyfor_reason'] : '';
				$save['refund_applyfor_date'] = date("Y-m-d H:i:s", time());
				$save['refund_applyfor_status'] = 1;
				
				$save = filterArr($save, array_keys($save), $oldData);
				$re = Db::table("gd_identify") -> where($where) -> update($save);
				if( !$re ) returnResults2('-1', '操作失败,请联系管理员');
				
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'用户申请鉴定售后（退货/退款）',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	
	/**
	 * 后台获取鉴定售后列表（退货/退款）
	 */
	public function getAdminRequestRefundList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where = [];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['pay_status', 'in', [1, 2]];
				$where[] = ['order_id', 'eq', 0];
				if( isset($in['create_date']) ) $where[] = ['create_date', 'between', $in['create_date']];
				if( isset($in['refund_applyfor_date']) ) $where[] = ['refund_applyfor_date', 'between', $in['refund_applyfor_date']];
				if( isset($in['refund_applyfor_status']) ) $where[] = ['refund_applyfor_status', 'eq', $in['refund_applyfor_status']];
				if( isset($in['status']) ) $where[] = ['status', 'eq', $in['status']];
				if( isset($in['isAfterSales']) && $in['isAfterSales'] == 1 ) {
					$where[] = ['refund_type', 'gt', 0];
					$where[] = ['refund_applyfor_status', 'gt', 0];
				}
				
				$data = Db::table("gd_identify") -> where($where) -> order("id desc") -> limit($start, $num) -> select();
				$count = Db::table("gd_identify") -> where($where) -> count();
				
				if( !empty($data) ){
					foreach ($data as $k => $v) {
						$data[$k]['gooodsName'] = '';
						$data[$k]['userPhone'] = Db::table("gd_user") -> where(['id'=>$v['create_user_id']]) -> find()['phone'];
						if( $v['goods_id'] > 0 ) $data[$k]['gooodsName'] = Db::table("gd_goods") -> where(['id'=>$v['goods_id']]) -> find()['name'];
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'后台获取鉴定售后列表（退货/退款）',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	
	/**
	 * 后台获取鉴定售后详情（退货/退款）
	 */
	public function getAdminRequestRefundDetails(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'identify_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where = [];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['id', 'eq', $in['identify_id']];
				
				$data = Db::table("gd_identify") -> where($where) -> find();
				
				if( !empty($data) ){
					foreach ($data as $k => $v) {
						$data['gooodsName'] = '';
						$data['userPhone'] = Db::table("gd_user") -> where(['id'=>$data['create_user_id']]) -> find()['phone'];
						if( $data['goods_id'] > 0 ) $data['gooodsName'] = Db::table("gd_goods") -> where(['id'=>$data['goods_id']]) -> find()['name'];
						$data['paymentRecords'] = Db::table("gd_payment_records") -> where(['identify_id'=>$data['id'], 'pay_status'=>1]) -> find();
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'后台获取鉴定售后详情（退货/退款）',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 后台审核鉴定售后（退货/退款）
	 */
	public function adminVerify(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'identify_id', 'refund_applyfor_status'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where = [];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['id', 'eq', $in['identify_id']];
				
				$oldData = Db::table("gd_identify") -> where($where) -> find();
				if( empty($oldData) ) returnResults2('-1', '操作失败,原数据有误');
				if( $oldData['refund_applyfor_status'] != 1 ) returnResults2('-1', '操作失败,原数据审核状态有误');
				
				Db::startTrans();
				
				$save['refund_applyfor_status'] = $in['refund_applyfor_status'];
				$save['refund_applyfor_status3_reason'] = isset($in['refund_applyfor_status3_reason']) ? $in['refund_applyfor_status3_reason'] : '';
				$save['refund_applyfor_status_date'] = date("Y-m-d H:i:s", time());
				if( $save['refund_applyfor_status'] == 2 ) $save['pay_status'] = 2;
				if( $save['refund_applyfor_status'] == 2 ) $save['status'] = 5;
				
				$re = Db::table("gd_identify") -> where($where) -> update($save);
				if( !$re ) {
					Db::rollback();
					returnResults2('-1', '变更鉴定数据失败,请联系管理员');
				}
				
				if( $save['refund_applyfor_status'] == 2 ){
					$refund_file = '';
					if( isset($in['refund_file']) ) $refund_file = is_array($in['refund_file']) ? json_encode($in['refund_file'], JSON_UNESCAPED_UNICODE) : $in['refund_file'];
					
					
					$save2['refund_status'] = 1;
					$save2['refund_price'] = $oldData['refund_applyfor_price'];
					$save2['refund_date'] = date("Y-m-d H:i:s", time());
					$save2['refund_type'] = isset($in['refund_type']) ? $in['refund_type'] : '';
					$save2['refund_file'] = $refund_file;
					$save2['refund_create_date'] = $oldData['refund_applyfor_date'];
					$save2['refund_desc'] = $oldData['refund_applyfor_reason'];
					
					$paymentRecordsWhere[] = ['identify_id', 'eq', $oldData['id']];
					$paymentRecordsWhere[] = ['pay_status', 'eq', 1];
					$re = Db::table("gd_payment_records") -> where($paymentRecordsWhere) -> update($save2);
					if( !$re ) {
						Db::rollback();
						returnResults2('-1', '变更鉴定支付记录数据失败,请联系管理员');
					}
				}
				Db::commit();
				
				$outArr['code'] = '1';
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'后台审核鉴定售后（退货/退款）',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	
	/**
	 * 用户填写邮寄单号
	 */
	public function userEditIdentify(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id', 'waybill_code1'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				$where[] = ['create_user_id', 'eq', $in['user_id']];
				$oldData = Db::table("gd_identify") -> where($where) -> find();
				if( empty($oldData) ) returnResults2('-1', '操作失败,原数据有误');
				if( !in_array($oldData['status'], [0, 4]) ) returnResults2('-1', '操作失败,鉴定状态有误,请联系管理员');
				
				$save['waybill_code1'] = $in['waybill_code1'];
				$save['date1'] = date("Y-m-d H:i:s", time());
				
				$save = filterArr($save, array_keys($save), $oldData);
				$re = Db::table("gd_identify") -> where($where) -> update($save);
				if( !$re ) returnResults2('-1', '操作失败,请联系管理员');
				
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'用户填写邮寄单号',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 后台审核撤销鉴定申请
	 */
//	public function verifyRevokeIdentify(){
//		if(request() -> isPost()){
//			$in = input("post.");
//			$bool = is_empty(['user_id', 'id', 'status'], $in);
//			if($bool){
//				$outArr['code'] = '40004';
//			}else{
//				$userInfo = $this -> getUserInfo();
//				
//				$where[] = ['id', 'eq', $in['id']];
//				$oldData = Db::table("gd_identify") -> where($where) -> find();
//				if( empty($oldData) ) returnResults2('-1', '操作失败,原数据有误');
//				if( !in_array($oldData['status'], [2]) ) returnResults2('-1', '操作失败,数据状态有误,请联系管理员');
//				
//				$save['status'] = $in['status'];
//				$save['revoke_notes'] = isset($in['revoke_notes']) ? $in['revoke_notes'] : '';
//				
//				$save = filterArr($save, array_keys($save), $oldData);
//				$re = Db::table("gd_identify") -> where($where) -> update($save);
//				if( !$re ) returnResults2('-1', '编辑失败,请联系管理员');
//				
//				$outArr['code'] = '1';
//				
////				写入操作日志
//				$old_data_json = $oldData;
//				controller("service/OperLog") -> logs(
//					[
//						'title'=>'后台审核撤销鉴定申请',
//						'business_type'=>3,
//						'oper_name'=>$userInfo['info']['nickname'],
//						'oper_user_id'=>$userInfo['info']['id'],
//						'json'=>[
//							[
//								'json_result'=>$outArr,
//								'old_data'=> isset($old_data_json) ? $old_data_json : [],
//							]
//						]
//					]
//				);
//			}
//		}else{
//			$outArr['code'] = '50001';
//		}
//		returnResults($outArr);
//	}
	
	/**
	 * 用户申请撤回鉴定
	 */
//	public function userRevokeIdentify(){
//		if(request() -> isPost()){
//			$in = input("post.");
//			$bool = is_empty(['user_id', 'id'], $in);
//			if($bool){
//				$outArr['code'] = '40004';
//			}else{
//				$userInfo = $this -> getUserInfo();
//				
//				$where[] = ['id', 'eq', $in['id']];
//				$where[] = ['create_user_id', 'eq', $in['user_id']];
//				$oldData = Db::table("gd_identify") -> where($where) -> find();
//				if( empty($oldData) ) returnResults2('-1', '操作失败,原数据有误');
//				if( !in_array($oldData['status'], [0, 4]) ) returnResults2('-1', '操作失败,只能申请撤回未鉴定的数据');
//				
//				$save['status'] = 2;
//				$save['user_revoke_notes'] = isset($in['user_revoke_notes']) ? $in['user_revoke_notes'] : '';
//				
//				$save = filterArr($save, array_keys($save), $oldData);
//				$re = Db::table("gd_identify") -> where($where) -> update($save);
//				if( !$re ) returnResults2('-1', '申请撤回失败,请联系管理员');
//				
//				$outArr['code'] = '1';
//				
////				写入操作日志
//				$old_data_json = $oldData;
//				controller("service/OperLog") -> logs(
//					[
//						'title'=>'用户申请撤回鉴定',
//						'business_type'=>3,
//						'oper_name'=>$userInfo['info']['nickname'],
//						'oper_user_id'=>$userInfo['info']['id'],
//						'json'=>[
//							[
//								'json_result'=>$outArr,
//								'old_data'=> isset($old_data_json) ? $old_data_json : [],
//							]
//						]
//					]
//				);
//			}
//		}else{
//			$outArr['code'] = '50001';
//		}
//		returnResults($outArr);
//	}
	
	/**
	 * 后台鉴定
	 */
	public function identify(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				$oldData = Db::table("gd_identify") -> where($where) -> find();
				if( empty($oldData) ) returnResults2('-1', '操作失败,原数据有误');
				
				foreach ($in as $k => $v) {
					if( !in_array($k, ['id', 'user_id']) ){
						$save[$k] = is_array($v) ? json_encode($v, JSON_UNESCAPED_SLASHES) : $v;
					}
				}
				$save['status'] = 1;
				
				$save = filterArr($save, array_keys($save), $oldData);
				if( !empty($save) ){
					$re = Db::table("gd_identify") -> where($where) -> update($save);
					if( !$re ) returnResults2('-1', '编辑失败,请联系管理员');
				}
				
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'后台鉴定',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 详情
	 */
	public function details(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				
				$data = Db::table("gd_identify") -> where($where) -> find();
				if( !empty($data) ){
					if( $data['goods_id'] > 0 ) $goods = Db::table("gd_goods") -> where(['id'=>$data['goods_id']]) -> find();
					$data['goods'] = isset($goods) ? $goods : [];
					$data['paymentRecords'] = Db::table("gd_payment_records") -> where(['identify_id'=>$data['id'], 'pay_status'=>1]) -> find();
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取鉴定详情',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 列表
	 */
	public function index(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				if( isset($in['status']) && !is_array($in['status']) ){
					$status = $in['status'];
					$in['status'] = [];
					$in['status'][] = $status;
				}
				
				$where = [];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['pay_status', 'in', [1, 2]];
//				$where[] = ['order_id', 'eq', 0];
				if( isset($in['create_date']) ) $where[] = ['create_date', 'between', $in['create_date']];
				if( isset($in['status']) ) $where[] = ['status', 'in', $in['status']];
				if( isset($in['isGetUserIdentify']) && $in['isGetUserIdentify'] == 1 ) $where[] = ['goods_id', 'eq', 0];
				if( isset($in['isGetGoodsIdentify']) && $in['isGetGoodsIdentify'] == 1 ) $where[] = ['goods_id', 'gt', 0];
				if( isset($in['searchUserId']) ) $where[] = ['create_user_id', 'eq', $in['searchUserId']];
				if( isset($in['create_user_name']) ) $where[] = ['create_user_name', 'like', '%'.$in['create_user_name'].'%'];
				if( isset($in['isAfterSales']) && $in['isAfterSales'] == 1 ) {
					$where[] = ['refund_type', 'gt', 0];
					$where[] = ['refund_applyfor_status', 'gt', 0];
				}
				
				$data = Db::table("gd_identify") -> where($where) -> order("id desc") -> limit($start, $num) -> select();
				$count = Db::table("gd_identify") -> where($where) -> count();
				
				if( !empty($data) ){
					foreach ($data as $k => $v) {
						$data[$k]['gooodsName'] = '';
						$data[$k]['userPhone'] = Db::table("gd_user") -> where(['id'=>$v['create_user_id']]) -> find()['phone'];
						if( $v['goods_id'] > 0 ) $data[$k]['gooodsName'] = Db::table("gd_goods") -> where(['id'=>$v['goods_id']]) -> find()['name'];
						
						$data[$k]['isShowAfterSales'] = FALSE;		//售后入口
						$data[$k]['isShowFillExpressDelivery'] = FALSE;		//填写快递入口
						if( $v['refund_type'] == 0 || in_array($v['refund_applyfor_status'], [3, 9]) ) $data[$k]['isShowAfterSales'] = TRUE;
						if( empty($v['waybill_code1']) && ($v['status'] == 0 && in_array($v['refund_applyfor_status'], [0, 3, 9])) ) $data[$k]['isShowFillExpressDelivery'] = TRUE;
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取鉴定列表',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 编辑地址
	 */
	public function edit(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				$oldData = Db::table("gd_identify") -> where($where) -> find();
				if( empty($oldData) ) returnResults(['code'=>'-1', 'msg'=>'原数据有误']);
				
				$save = [];
				foreach ($in as $k => $v) {
					if( !in_array($k, ['id', 'user_id']) ){
						$save[$k] = is_array($v) ? json_encode($v, JSON_UNESCAPED_SLASHES) : $v;
					}
				}
				
				$save = filterArr($save, array_keys($save), $oldData);
				
				if( !empty($save) ){
					$re = Db::table("gd_identify") -> where($where) -> update($save);
					if( !$re ) returnResults2('-1', '编辑失败,请联系管理员');
				}
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'编辑鉴定',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 新增
	 */
	public function add(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$newData = [];
				foreach ($in as $k => $v) {
					if( !in_array($k, ['id', 'user_id']) ){
						$newData[$k] = is_array($v) ? json_encode($v, JSON_UNESCAPED_UNICODE) : $v;
					}
				}
				$newData['create_user_id'] = $in['user_id'];
				$newData['create_user_name'] = $userInfo['info']['nickname'];
				$newData['create_user_type'] = $userInfo['info']['type'];
				$newData['create_date'] = date("Y-m-d H:i:s", time());
				$newData['date1'] = '0000-00-00 00:00:00';
				$newData['date2'] = '0000-00-00 00:00:00';
				$newData['identify_date'] = '0000-00-00 00:00:00';
				
				Db::startTrans();
				$newIdentifyId = Db::table("gd_identify") -> insertGetId($newData);
				if( !$newIdentifyId ) {
					Db::rollback();
					returnResults2('-1', '新增失败,请联系管理员');
				}
				
				if( $newData['appralsal_fee'] > 0 ){
//					统一下单
					$goodsName = $newData['describe'];
					$totalPrice = $newData['appralsal_fee'] * 100;
					$openId = $userInfo['info']['open_id'];
					$attach = ['payType'=>2, 'identifyId'=>$newIdentifyId];
					$re = controller("service/Wechat") -> unifiedOrder($goodsName, $totalPrice, $openId, $attach, $userInfo['info']);
					if( $re['code'] != '1' ){
						Db::rollback();
						returnResults(['code'=>'-1', 'msg'=>'统一下单操作失败,请联系管理员', 'info'=>$re]);
					}
					$outArr['info'] = $re['info'];
				}
				Db::commit();
				
				$outArr['code'] = '1';
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'新增鉴定',
						'business_type'=>1,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 微信统一下单（待支付的鉴定）
	 */
	public function unifiedOrderState2(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'identify_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
//				获取订单
				$where[] = ['id', 'eq', $in['identify_id']];
				$oldData = Db::table("gd_identify") -> where($where) -> find();
				if( empty($oldData) ) returnResults2('-1', '操作失败,查询鉴定数据有误,请联系管理员');
				
//				统一下单
				$goodsName = $oldData['describe'];
				$totalPrice = ($oldData['appralsal_fee'] + $oldData['milage']) * 100;
				$openId = $userInfo['info']['open_id'];
				$attach = ['payType'=>1, 'identify_id'=>$oldData['id']];
				$re = controller("service/Wechat") -> unifiedOrder($goodsName, $totalPrice, $openId, $attach, $userInfo['info']);
				if( $re['code'] != '1' ){
					returnResults2('-1', '统一下单操作失败,请联系管理员');
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $re['info'];
				
//				写入操作日志
				$old_data_json = $order;
				controller("service/OperLog") -> logs(
					[
						'title'=>'微信统一下单（待支付的鉴定）',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
}

?>